<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    function GetDateDiff(startTime, endTime, diffType) {
        //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
        startTime = startTime.replace(/\-/g, "/");
        endTime = endTime.replace(/\-/g, "/");
        //将计算间隔类性字符转换为小写
        diffType = diffType.toLowerCase();
        let sTime = new Date(startTime); //开始时间
        let eTime = new Date(endTime); //结束时间
        console.log('start:', sTime)
        console.log('end:', eTime)
        //作为除数的数字
        let timeType = 1;
        switch (diffType) {
            case"second":
                timeType = 1000;
                break;
            case"minute":
                timeType = 1000 * 60;
                break;
            case"hour":
                timeType = 1000 * 3600;
                break;
            case"day":
                timeType = 1000 * 3600 * 24;
                break;
            default:
                break;
        }
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
    }

    const a = GetDateDiff("2018-05-27 01:20:22", "2018-07-27 12:20:22", "day")

    console.log(a)
</script>
</body>
</html>